package com.wx.mythread.stopthread.volatiledemo;

/**
 * 看似可行的Volatile变量停止线程，看似可以，但是实际上有很大的局限性
 */
public class WrongWayVolatile implements Runnable{

    public static volatile boolean canceled = false;

    public static void main(String[] args) throws InterruptedException {
        WrongWayVolatile r = new WrongWayVolatile();
        Thread thread = new Thread(r);
        thread.start();
        Thread.sleep(5000);
        WrongWayVolatile.canceled=true;
    }


    @Override
    public void run() {
        int num = 0;
        try {
            while (num < 10000 && !canceled){
                if (num % 100 == 0){
                    System.out.println(num + "是100的倍数");
                }
                num++;
                Thread.sleep(10);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
